﻿ 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation;

namespace Simon
{
    public partial class Simon : SurfaceUserControl
    {
        private string CurrentScore = "00";
        private string TopScore = "00";
        private List<int> CurrentGame = new List<int>();
        private List<int> CurrentPlayer = new List<int>();
        private int CurrentPosition = 0;
        private int PlayerPosition = 0;
        private bool GameLoopActive = false;

        public Simon()
        {
            InitializeComponent();

            SimonSounds.AutoPlay = true; 
        } 

        #region preferences button
 
        private void PrefsBtn_Click(object sender, RoutedEventArgs e)
        {
            PlaySound("Audio/SmallBtnClick.mp3");

            #if Silverlight
                PrefsBtnPressed.Begin();
            #else
                Storyboard foobar = TopSimonGrid.FindResource("PrefsBtnPressedKey") as Storyboard;
                foobar.Begin();
            #endif 

            SilverligthFX1.ExecuteFlip();
        }

        private void PrefsStateTimer_Completed(object sender, EventArgs e) { }

        #endregion

        #region scores button 

        private void ScoresBtn_Click(object sender, RoutedEventArgs e)
        {
            PlaySound("Audio/SmallBtnClick.mp3");

            #if Silverlight
                NewBtnPressed.Begin();
            #else
                Storyboard foobar = TopSimonGrid.FindResource("NewBtnPressedKey") as Storyboard;
                foobar.Begin();
            #endif

            FormatScoreBoard(int.Parse(TopScore));
        }

        private void TopScoresTimer_Completed(object sender, EventArgs e)
        {
            FormatScoreBoard(int.Parse(CurrentScore));

            #if Silverlight
                PrefsBtnPressed.Begin();
            #else
                Storyboard foobar = TopSimonGrid.FindResource("PrefsBtnPressedKey") as Storyboard;
                foobar.Begin();
            #endif 
        }

        #endregion

        #region new button 

        private void NewBtn_Click(object sender, RoutedEventArgs e)
        {
            PlaySound("Audio/SmallBtnClick.mp3");
            FormatScoreBoard(0);

            #if Silverlight
                NewBtnPressed.Begin();
                StartGameTimer.Begin();
            #else
                Storyboard foobar = TopSimonGrid.FindResource("PrefsBtnPressedKey") as Storyboard;
                foobar.Begin();
                Storyboard foobar2 = TopSimonGrid.FindResource("StartGameTimerKey") as Storyboard;
                foobar2.Begin();
            #endif 
        }

        private void StartGameTimer_Completed(object sender, EventArgs e)
        {
            CurrentPosition = 0;
            PlayerPosition = 0;
            CurrentGame = new List<int>();
            CurrentPlayer = new List<int>();
            ButtonByIndex(RandomNumber(1, 5));
        }

        #endregion

        private void ButtonByIndex(int Indexer)
        {
            CurrentGame.Add(Indexer);
            CurrentPosition = CurrentPosition + 1;
            PlayerPosition = 0;

            ButtonFireAnimation(Indexer); //tool
        }

        private void Sound_MediaEnded(object sender, RoutedEventArgs e)
        {
            ((MediaElement)sender).Stop();
            ((MediaElement)sender).Position = new TimeSpan(00);
        }

        /// <summary>
        /// main game logic fires on each game pad button click
        /// </summary>
        /// <param name="PressButId"></param>
        private void CheckButtonPress(int PressButId)
        {
            //PlaySound("Audio/PadButtonClick.mp3");

            if (CurrentGame.Count != 0)
            {
                if (CurrentGame[PlayerPosition] != PressButId)
                {
                    PlaySound("Audio/Fail.mp3");
                    FormatScoreBoard(int.Parse(CurrentScore));

                    #if Silverlight
                        NewBtnPressed.Begin(); 
                    #else
                        Storyboard foobar = TopSimonGrid.FindResource("NewBtnPressedKey") as Storyboard;
                        foobar.Begin(); 
                    #endif

                    // blow up... 
                    return;
                }

                PlayerPosition = PlayerPosition + 1;
                if (PlayerPosition == CurrentGame.Count)
                {
                    CurrentPlayer.Add(PressButId);

                    // current score 
                    int Temp = PlayerPosition;
                    CurrentScore = Temp.ToString();

                    // top score 
                    if (int.Parse(TopScore) < Temp)
                    {
                        TopScore = CurrentScore;
                    }

                    // current score
                    if (Temp > int.Parse(ActiveScoreCount.Text))
                    {
                        FormatScoreBoard(int.Parse(CurrentScore));
                    }

                    PlayerPosition = 0;

                    CurrentPosition = 0;
                    CurrentGame.Add(RandomNumber(1, 5));

                    #if Silverlight
                        GameLoopStart.Begin();
                    #else
                        Storyboard foobar = TopSimonGrid.FindResource("GameLoopStartKey") as Storyboard;
                        foobar.Begin(); 
                    #endif 
                }
                else
                {
                    StartLoop();
                }
            }
        }

        private void StartLoop()
        {
            if (CurrentPosition < CurrentGame.Count && !GameLoopActive)
            {
                GameLoopActive = true;
                ButtonFireAnimation(CurrentGame[CurrentPosition]);

                CurrentPosition = CurrentPosition + 1;

                #if Silverlight
                    GameLoop.Begin();
                #else
                    Storyboard foobar = TopSimonGrid.FindResource("GameLoopKey") as Storyboard;
                    foobar.Begin(); 
                #endif  
            }
        }

        private void GameLoop_Completed(object sender, EventArgs e)
        {
            GameLoopActive = false;
            StartLoop();
        }

        #region Game Pad Button event handlers

        private void Green_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            #if Silverlight
                GreenBtnPressed.Begin();
            #else
                Storyboard foobar = TopSimonGrid.FindResource("GreenBtnPressedKey") as Storyboard;
                foobar.Begin(); 
            #endif

            CheckButtonPress(1);
            PlaySound("Audio/tone1.mp3");
        }

        private void Red_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            #if Silverlight
                RedBtnPressed.Begin();
            #else
                Storyboard foobar = TopSimonGrid.FindResource("RedBtnPressedKey") as Storyboard;
                foobar.Begin(); 
            #endif

            CheckButtonPress(2);
            PlaySound("Audio/tone2.mp3");
        }

        private void BlueBtn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            #if Silverlight
                BlueBtnPressed.Begin();
            #else
                Storyboard foobar = TopSimonGrid.FindResource("BlueBtnPressedKey") as Storyboard;
                foobar.Begin(); 
            #endif

            CheckButtonPress(4);
            PlaySound("Audio/tone4.mp3");
        }

        private void YellowBtn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            #if Silverlight
                YellowBtnPressed.Begin();
            #else
                Storyboard foobar = TopSimonGrid.FindResource("YellowBtnPressedKey") as Storyboard;
                foobar.Begin(); 
            #endif

            CheckButtonPress(3);
            PlaySound("Audio/tone3.mp3");
        }

        private void FrontBtn_ContactUp(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
        {
            PrefsBtn_Click(sender, null);
        }

        private void GreenBtn2_Click(object sender, RoutedEventArgs e)
        {
            Green_MouseLeftButtonUp(sender, null);
        }

        private void RedBtn2_Click(object sender, RoutedEventArgs e)
        {
            Red_MouseLeftButtonUp(sender, null);
        }

        private void BlueBtn2_Click(object sender, RoutedEventArgs e)
        {
            BlueBtn_MouseLeftButtonUp(sender, null);
        }

        private void YellowBtn2_Click(object sender, RoutedEventArgs e)
        {
            YellowBtn_MouseLeftButtonUp(sender, null);
        }

        #endregion

        #region [ utilities ]

        private void PlaySound(string SoundFile)
        {
            SimonSounds.SetSource(SoundFile);
        }

        private void FormatScoreBoard(int ScoreValue)
        {
            if (ScoreValue < 10)
            {
                ActiveScoreCount.Text = "0" + ScoreValue.ToString();
            }
            else if (ScoreValue < 100)
            {
                ActiveScoreCount.Text = ScoreValue.ToString();
            }
            else
            {
                ActiveScoreCount.Text = "99";
            }
        }

        /// <summary>
        /// fires the animation for the selected button on game pad
        /// </summary>
        /// <param name="BtnIndex">int value 1 through 4</param>
        private void ButtonFireAnimation(int BtnIndex)
        {
            switch (BtnIndex)
            {
                case 1:
                    #if Silverlight
                        GreenBtnPressed.Begin();
                    #else
                        Storyboard foobar = TopSimonGrid.FindResource("GreenBtnPressedKey") as Storyboard;
                        foobar.Begin(); 
                    #endif

                    PlaySound("Audio/tone1.mp3");
                    break;
                case 2:
                    #if Silverlight
                        RedBtnPressed.Begin();
                    #else
                        Storyboard foobar2 = TopSimonGrid.FindResource("RedBtnPressedKey") as Storyboard;
                        foobar2.Begin(); 
                    #endif 

                    PlaySound("Audio/tone2.mp3");
                    break;
                case 3:
                    #if Silverlight
                        YellowBtnPressed.Begin();
                    #else
                        Storyboard foobar3 = TopSimonGrid.FindResource("YellowBtnPressedKey") as Storyboard;
                        foobar3.Begin(); 
                    #endif

                    PlaySound("Audio/tone3.mp3");
                    break;
                case 4:
                    #if Silverlight
                        BlueBtnPressed.Begin();
                    #else
                        Storyboard foobar4 = TopSimonGrid.FindResource("BlueBtnPressedKey") as Storyboard;
                        foobar4.Begin(); 
                    #endif
                    
                    PlaySound("Audio/tone4.mp3");
                    break;
            }
        }

        /// <summary>
        /// returns a random number of type int between min and max inputs values.  should be 1 and 5 which can then be 1,2,3 or 4
        /// </summary>
        /// <param name="min">int</param>
        /// <param name="max">int</param>
        /// <returns>int</returns>
        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        #endregion

    }
}
